package com.sgb.kjwl.viewmodel.self.packers;

import android.util.Log;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.ResponseList;
import com.sgb.kjwl.model.entity.self.SelfTeamAchieveEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamDeviceEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamIntroduceEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamMemberEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamOfferEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamActivity;
import com.sgb.kjwl.view.ui.fragment.self.FragPackersManageFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FragPackersManageModel extends BaseViewModel {

    private FragPackersManageFragment fragment;
    private Map<String, Object> map = new HashMap<>();
    private int mCurPager = 1;

    public FragPackersManageModel(FragPackersManageFragment fragment, SelfRepertory selfRepertory) {
        this.fragment = fragment;
    }

    /*=========================团队人员==============================*/

    public void requestMemberList(boolean isLoadMore) {
        map.clear();
        mCurPager = isLoadMore ? mCurPager + 1 : 1;
        map.put("page", mCurPager);
        map.put("limit", AppConstant.PAGER_LIMIT);
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.put("companyNo", "");
            map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else {
            map.put("companyNo", companyNo);
            map.put("userNo", "");
        }
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getTeamMember(map).enqueue(new Callback<BaseEntity<ResponseList<List<SelfTeamMemberEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ResponseList<List<SelfTeamMemberEntity>>>> call, Response<BaseEntity<ResponseList<List<SelfTeamMemberEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showMemberList(response.body().getData(), isLoadMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ResponseList<List<SelfTeamMemberEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void deleteTeamMember(String no) {
        map.clear();
        map.put("no", no);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().deleteTeamMember(map)
                .enqueue(new Callback<BaseEntity<Object>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                        DialogHelper.dismissProgressDialog();
                        BaseEntity<Object> body = response.body();
                        if (body == null || body.getState() == null) {
                            return;
                        }
                        if (body.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            requestMemberList(false);
                        } else {
                            MToast.showToast(fragment.getContext(), body.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    /*=========================工种报价==============================*/

    public void requestOfferList(boolean isLoadMore) {
        map.clear();
        mCurPager = isLoadMore ? mCurPager + 1 : 1;
        map.put("page", mCurPager);
        map.put("limit", AppConstant.PAGER_LIMIT);
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.put("companyNo", "");
            map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else {
            map.put("companyNo", companyNo);
            map.put("userNo", "");
        }
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getTeamPrice(map).enqueue(new Callback<BaseEntity<ResponseList<List<SelfTeamOfferEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ResponseList<List<SelfTeamOfferEntity>>>> call, Response<BaseEntity<ResponseList<List<SelfTeamOfferEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showOfferList(response.body().getData(), isLoadMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ResponseList<List<SelfTeamOfferEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void deleteTeamOffer(String no) {
        map.clear();
        map.put("no", no);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().deleteTeamPrice(map)
                .enqueue(new Callback<BaseEntity<Object>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                        DialogHelper.dismissProgressDialog();
                        BaseEntity<Object> body = response.body();
                        if (body == null || body.getState() == null) {
                            return;
                        }
                        if (body.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            requestOfferList(false);
                        } else {
                            MToast.showToast(fragment.getContext(), body.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                        MToast.showToast(fragment.getContext(), "删除失败！");
                    }
                });
    }

    /*=========================专业设备==============================*/

    public void requestDeviceList(boolean isLoadMore) {
        map.clear();
        mCurPager = isLoadMore ? mCurPager + 1 : 1;
        map.put("page", mCurPager);
        map.put("limit", AppConstant.PAGER_LIMIT);
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.put("companyNo", "");
            map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else {
            map.put("companyNo", companyNo);
            map.put("userNo", "");
        }
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getTeamDeviceList(map).enqueue(new Callback<BaseEntity<ResponseList<List<SelfTeamDeviceEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ResponseList<List<SelfTeamDeviceEntity>>>> call, Response<BaseEntity<ResponseList<List<SelfTeamDeviceEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showDeviceList(response.body().getData(), isLoadMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ResponseList<List<SelfTeamDeviceEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void deleteTeamDevice(String no) {
        map.clear();
        map.put("no", no);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据删除中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().delTeamDeviceList(map)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            MToast.showToast(fragment.getContext(), "删除成功");
                            requestDeviceList(false);
                        } else {
                            assert response.body() != null;
                            MToast.showToast(fragment.getContext(), response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    /*=========================工程业绩==============================*/
    public void requestAchieveList(boolean isLoadMore) {
        map.clear();
        mCurPager = isLoadMore ? mCurPager + 1 : 1;
        map.put("page", mCurPager);
        map.put("limit", AppConstant.PAGER_LIMIT);
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.put("companyNo", "");
            map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else {
            map.put("companyNo", companyNo);
            map.put("userNo", "");
        }
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getTeamResult(map).enqueue(new Callback<BaseEntity<ResponseList<List<SelfTeamAchieveEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ResponseList<List<SelfTeamAchieveEntity>>>> call, Response<BaseEntity<ResponseList<List<SelfTeamAchieveEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showAchieveList(response.body().getData(), isLoadMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ResponseList<List<SelfTeamAchieveEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }


    public void deleteTeamAchieve(String no) {
        map.clear();
        map.put("no", no);
        APIManager.getInstance().getUserAPI().deleteTeamResult(map)
                .enqueue(new Callback<BaseEntity<Object>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                        DialogHelper.dismissProgressDialog();
                        BaseEntity<Object> body = response.body();
                        if (body == null || body.getState() == null) {
                            return;
                        }
                        if (body.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            requestAchieveList(false);
                        } else {
                            MToast.showToast(fragment.getContext(), body.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                        MToast.showToast(fragment.getContext(), "删除失败！");
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    /*=========================H5==============================*/
    public void getH5Url(String keyWord, String no) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(keyWord).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + no + "&isdele=1";
                    Log.e("tag用户中心工队业绩",url+"" );
                    WebViewX5HostActivity.start(fragment.getContext(), url, SelfTeamActivity.REQUEST_CODE);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getOfferH5Url(String keyWord, String no) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(keyWord).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + no + "&type=0";
                    WebViewX5HostActivity.start(fragment.getContext(), url, SelfTeamActivity.REQUEST_CODE);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    // -------------------------------------------工队简介-------------------------------------------

    public void getTeamIntroduce() {
        map.clear();
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.put("companyNo", "");
            map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else {
            map.put("companyNo", companyNo);
            map.put("userNo", "");
        }
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getTeamIntroduce(map).enqueue(new Callback<BaseEntity<SelfTeamIntroduceEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<SelfTeamIntroduceEntity>> call, Response<BaseEntity<SelfTeamIntroduceEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    fragment.updateIntroduce(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<SelfTeamIntroduceEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
